package com.biejiaogroup.librarymanager.controller;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;


@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {

            response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            DiskFileItemFactory factory = new DiskFileItemFactory();
            System.out.println(getServletContext().getRealPath("src/headshots/temp"));
            File f = new File(getServletContext().getRealPath("src/headshots/temp"));
            if(!f.exists()) f.mkdirs();
            factory.setRepository(f);

            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            fileUpload.setHeaderEncoding("utf-8");
            List<FileItem> fileItems = fileUpload.parseRequest(request);

            PrintWriter out = response.getWriter();

            String userID = null;
            for(FileItem item : fileItems)
            {

                if(item.isFormField())
                {
                    String name = item.getFieldName();

                    if(name.equals("userID"))
                    {
                        if(!item.getString().equals(""))
                        {
                            userID = item.getString();
                            System.out.println(userID);
                        }
                    }
                }
                else
                {
                    String fileName = item.getName();
                    if(fileName != null && !fileName.equals(""))
                    {
                        System.out.println(fileName);
                        //将userID作为文件名
                        String filePath = "/src/headshots/" + userID + ".jpg";
                        filePath = getServletContext().getRealPath(filePath);

                        System.out.println(filePath);

                        File file = new File(filePath);
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                        InputStream in = item.getInputStream();
                        FileOutputStream out1 = new FileOutputStream(file);
                        byte[] buffer = new byte[1024];
                        int len;
                        while((len = in.read(buffer)) > 0)
                        {
                            out1.write(buffer, 0, len);
                        }
                        in.close();
                        out1.close();
                        item.delete();
                        out.write("success");
                    }
                }
            }


        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

